<template>
  <div class="bg">
    <div class="login-box">
      <div class="title">
        后台管理系统
      </div>
      <div class="for-input">
        <label>
          账号
        </label>
        <input type="text" v-model="params.loginname" placeholder="请输入账号">
      </div>
      <div class="for-input">
        <label>
          密码
        </label>
        <input type="password" v-model="params.pwd" placeholder="请输入密码">
      </div>
      <div class="form-btn">
        <button type="button" @click="subForm">登录</button>
      </div>
    </div>
    <div class="stars">
    </div>
    <messageDialog v-model="visible"></messageDialog>
  </div>
</template>

<script>
    import messageDialog from '@/components/dialog/messageDialog'
    export default {
        name: "Login",
        data(){
          return{
            params:{
              "loginname":"",
              "pwd":""
            },
            visible: false,
            navType:"",
          }
        },
        components:{
          messageDialog
        },
        computed:{
          getMessageObj () {
            return this.$store.state.messageDialogObj;
          },
        },
        watch:{
          getMessageObj: {
            handler: function(val){
              this.visible = val.visible;
              this.txt = val.txt;
            },
            deep:true
          },
        },
        mounted(){
          //console.log(this.$route.query);
          this.navType = this.$route.query.navType ? this.$route.query.navType : "";
          sessionStorage.setItem("navType",this.navType);
          //console.log(sessionStorage.getItem("navType"));
          $(document).ready(function () {
            var stars = 800;
            var $stars = $('.stars');
            var r = 800;
            for (var i = 0; i < stars; i++) {
              if (window.CP.shouldStopExecution(1)) {
                break;
              }
              var $star = $('<div/>').addClass('star');
              $stars.append($star);
            }
            window.CP.exitedLoop(1);
            $('.star').each(function () {
              var cur = $(this);
              var s = 0.2 + Math.random() * 1;
              var curR = r + Math.random() * 300;
              cur.css({
                transformOrigin: '0 0 ' + curR + 'px',
                transform: ' translate3d(0,0,-' + curR + 'px) rotateY(' + Math.random() * 360 + 'deg) rotateX(' + Math.random() * -50 + 'deg) scale(' + s + ',' + s + ')'
              });
            });
          });
        },
        methods:{
          subForm(){
            if( this.params.loginname === "" ){
              this.s_alert("登录名不能为空！");
              return false;
            }else if( this.params.pwd === "" ){
              this.s_alert("密码不能为空！");
              return false;
            }

            let params =  {
              "columns": ["*"],
              "conditions": [ //查新条件
                {
                  "field": "loginname",
                  "symbol": "=",
                  "value": this.params.loginname
                },
                {
                  "field": "pwd",
                  "symbol": "=",
                  "value": this.params.pwd
                }
              ],
              "dataTypeCode": "userprofile", //数据库选择
              "pageIndex": 1, //分页
              "pageSize": -1, //分页条目 -1查所有
              "tableName": "T_PRIV_USER" //表名称
            };

            this.$axios.post(this.HOST+"/base/query/page",params)
              .then((data)=>{
                let userData = data.data.data.list;
                //console.log(userData);
               // //console.log(userData[0].STATE);
                if( userData.length >= 1 ){
                  if( userData[0].STATE === 1 ){
                    this.s_alert("登录成功");
                    //console.log(this.navType === 'ZDR');
                    if( this.navType === 'ZDR' ){
                      this.$router.push({path:"/keyPersonListController"});
                    }else{
                      this.$router.push({path:"/"});
                    }
                    const userSession = {
                      "userName":userData[0].USERNAME,
                      "userId":userData[0].ID,
                      "loginName":userData[0].LOGINNAME,
                    };
                    //console.log(userSession);
                    sessionStorage.setItem("userSession",JSON.stringify(userSession));
                    this.s_logwrite("登录页面","登录");
                  }else if( userData[0].STATE === 0 ){
                    this.s_alert("该账号已被禁用，请联系管理员！");
                  }
                }else if( userData.length ===0 ){
                  this.s_alert("账号不存在或密码错误！");
                }
              })
              .catch((err)=>{
                //console.log(err);
                this.s_alert("登录失败!");
              })
          }
        }
    }
</script>

<style scoped>
  .bg {
    background: radial-gradient(200% 100% at bottom center, #0070aa, #0b2570, #000035, #000);
    background: radial-gradient(220% 105% at top center, #000 10%, #000035 40%, #0b2570 65%, #0070aa);
    background-attachment: fixed;
    overflow: hidden;
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  @keyframes rotate {
    0% {
      transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(0);
    }
    100% {
      transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(-360deg);
    }
  }
  .stars {
    transform: perspective(500px);
    transform-style: preserve-3d;
    position: absolute;
    bottom: 0;
    perspective-origin: 50% 100%;
    left: 50%;
    animation: rotate 90s infinite linear;
  }
</style>
